package com.xzh.service;

import com.xzh.bean.Employee;
import com.xzh.bean.EmployeeExample;
import com.xzh.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author XuZhuHong
 * @CreateTime 2021/6/1 12:13
 */
@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    public List< Employee > getAll() {
        //查询所有员工
        List< Employee > employees = employeeMapper.selectByExampleWidthDept(null);

        return employees;
    }

    //保存员工
    public void saveEmp(Employee employee) {
        employeeMapper.insertSelective(employee);
    }

    //检查用户是否可用
    public boolean checkUser(String empName) {

        //创建条件查询
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        //添加条件
        criteria.andEmpNameEqualTo(empName);
        //开始查询
        long l = employeeMapper.countByExample(example);
        if (l > 0) {
            return false;
        }
        return true;
    }

    public Employee getEmp(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }
    //更新方法
    public void updateEmp(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    public void deleteEmp(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    public void deleteEmps(List<Integer> split) {
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        //下面这句话后
        // delete from xXx where emp_ id in(1,2,3)
        criteria.andEmpIdIn(split);
        employeeMapper.deleteByExample(employeeExample);
    }
}
